﻿// ===============================================================================
// XCI.NET 西安交通信息WinForm开发框架 2018 版权所有
// ===============================================================================

using System;
using System.Globalization;
using System.Net.Http;
using XCI.Core;
using XCI.Helper;

namespace XCI.Sys
{
    /// <summary>
    /// 系统Api接口
    /// </summary>
    public class SysWebApi : WebApi
    {
        private string _appId;

        /// <summary>
        /// 初始化系统Api接口
        /// </summary>
        /// <param name="appId">应用Id</param>
        /// <param name="baseUrl">服务器基地址</param>
        /// <param name="timeout">超时间隔</param>
        /// <param name="useProxy">是否使用代理</param>
        public SysWebApi(string appId, string baseUrl, TimeSpan timeout, bool useProxy) : base(baseUrl, timeout, useProxy)
        {
            _appId = appId;
        }

        /// <summary>
        /// 在请求前触发
        /// </summary>
        /// <param name="request">请求对象</param>
        /// <returns>如果返回false,则终止请求</returns>
        protected override bool OnBeforeRequest(HttpRequestMessage request)
        {
            request.Headers.Add("appId", _appId);//app
            request.Headers.Add("timestamp", DateTimeHelper.GetTimeStamp().ToString(CultureInfo.InvariantCulture));//时间戳
            request.Headers.Add("token", SysRuntime.CurrentUser?.Token);//token
            return true;
        }

        ///// <summary>
        ///// 获取JwtToken
        ///// </summary>
        //protected string GetToken()
        //{
        //    string key = SysConst.JwtTokenCacheKey;
        //    var value = MemoryCache.Default.Get(key);
        //    if (value != null && !string.IsNullOrEmpty(value.ToString())) return value.ToString();
        //    return value?.ToString();
        //}
    }
}